import { dayjs } from '@/main.js';
import { getDicts } from '@/api/system/dict/data';
var mixins = {
	created() {},
	mounted() {},
	data() {
		return {};
	},
	methods: {
		_randomString(len) {
			len = len || 32;
			let timestamp = new Date().getTime();
			/****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
			let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
			let maxPos = $chars.length;
			let randomStr = '';
			for (let i = 0; i < len; i++) {
				randomStr += $chars.charAt(Math.floor(Math.random() * maxPos));
			}

			return randomStr + timestamp;
		},

		_previewImages(urls) {
			this.$imagePreview({
				urlList: urls,
			}).then(res => {});
		},
		_toPage(route, type = 'push') {
			if (!this.notEmpty(route)) return;
			if (this.$store.state.tagsView.cachedViews?.includes(route.name)) {
				this.$nextTick(() => {
					this.$tab.refreshPage(route);
				});
			} else {
				switch (type) {
					case 'push':
						this.$router.push(route);
						break;
					case 'replace':
						this.$router.replace(route);
						break;
					case 'go':
						this.$router.go(route);
						break;
					default:
						break;
				}
			}
		},
		_set(row, key, value) {
			this.$set(row, key, value);
		},
		_formatTime(time, reg) {
			switch (reg) {
				case 0:
					reg = 'YYYY-MM-DD';
					break;
				case 1:
					reg = 'YYYY-MM-DD HH:mm:ss';
					break;
			}
			let pass = dayjs(time).isValid();
			if (!pass) return time;

			return dayjs(time).format(reg);
		},
		_getDicts(data) {
			if (!data) Promise.resolve([]);
			return new Promise((resolve, reject) => {
				getDicts(data)
					.then(response => {
						resolve(response.data ?? []);
					})
					.catch(() => {
						resolve([]);
					});
			});
		},

		_valid(form, event = 'validate') {
			return new Promise(resolve => form[event](valid => resolve(valid)));
		},
		_validate() {
			return new Promise(async resolve => {
				let result = true;
				let refs = Object.values(this.$refs);
				for (let index = 0; index < refs?.length; index++) {
					const ref = refs[index];
					if (
						Object.prototype.toString.call(ref) == '[object Array]'
					) {
						for (let i = 0; i < ref.length; i++) {
							const element = ref[i];
							if (element?._validate) {
								let rc = await element?._validate?.();
								result = result && rc;
							}
							if (element?.validate) {
								let rf = await this._valid(ref);
								result = result && rf;
							}
							if (element?.form_validate) {
								let rf = await this._valid(
									ref,
									'form_validate',
								);
								result = result && rf;
							}
						}
					} else {
						if (ref?._validate) {
							let rc = await ref?._validate?.();
							result = result && rc;
						}
						if (ref?.validate) {
							let rf = await this._valid(ref);
							result = result && rf;
						}
						if (ref?.form_validate) {
							let rf = await this._valid(ref, 'form_validate');
							result = result && rf;
						}
					}
				}
				resolve(result);
			});
		},
		_clearValid() {
			let refs = Object.values(this.$refs);
			for (let index = 0; index < refs?.length; index++) {
				const ref = refs[index];
				if (Object.prototype.toString.call(ref) == '[object Array]') {
					for (let i = 0; i < ref.length; i++) {
						const element = ref[i];
						if (element?._clearValid) {
							element?._clearValid?.();
						}
						if (element?.clearValidate) {
							element.clearValidate?.();
						}
						if (element?.form_clearValidate) {
							element.form_clearValidate?.();
						}
					}
				} else {
					if (ref?._clearValid) {
						ref?._clearValid?.();
					}
					if (ref?.clearValidate) {
						ref.clearValidate?.();
					}
					if (ref?.form_clearValidate) {
						ref.form_clearValidate?.();
					}
				}
			}
		},
		_closeCurrentPage() {
			this.$tab.closeOpenPage(this.route);
		},
	},
	computed: {},
	filters: {
		_setEmpty(row) {
			if (row === undefined || row === null || row === '') {
				return '-';
			} else {
				return row;
			}
		},
		_getChain(row, prop) {
			if (
				Object.prototype.toString.call(prop) !== '[object String]' &&
				Object.prototype.toString.call(prop) !== '[object Array]'
			) {
				return '--';
			}
			let data = [];
			if (typeof prop === 'string') {
				data = prop.split('.');
			}
			let result = data.reduce(function (prev, next) {
				return prev?.[next];
			}, row);
			return result ?? '--';
		},
		_getIDCard(data) {
			let res = '--';
			res =
				data?.replace(/^(.{6})(?:\w+)(.{4})$/, '$1********$2') ?? '--';

			return res;
		},
		_translateDict(value, dictList) {
			if (!dictList) return value;
			let dict = dictList.find(
				item => (item.value ?? item.dictValue) === value,
			);
			if (dict) return dict.label ?? dict.dictLabel;
			return '-';
		},
	},
};

export default mixins;
